import {toArray} from 'pub-bbx-utils';
import { isArray } from '@src/util/type'
import { expandField } from '@src/component/form/util'
import { formatAddress } from 'pub-bbx-utils';
/** 将form对象转成客户对象，用于提交表单 */
export function packToCustomer(fields, form, initTags){
  let customer = {
    id: form.id,
    attribute: {},
  };
  
  if (isArray(form.customerTag)) {
    customer.tenantTagList = form.customerTag.map(tag => ({ id: tag.id || tag }))
  }
  
  expandField(fields).forEach(field => {
    let {fieldName, isSystem} = field;
    let value = form[fieldName];
    
    if(fieldName === 'customerAddress'){
      let customerAddress = form.customerAddress || {};
      // address
      return customer.customerAddress = {
        adCountry:customerAddress.country || '',
        adProvince: customerAddress.province,
        adCity: customerAddress.city,
        adDist: customerAddress.dist,
        adLatitude: customerAddress.latitude || '',
        adLongitude: customerAddress.longitude || '',
        addressType: customerAddress.addressType || 0,
        adAddress: customerAddress.address,
        adStreet: customerAddress.street || '',
      };
    }

    if(fieldName == 'tags'){
      let tags = Array.isArray(value) ? value : [];
      // COMMENT: 暂时去除团队
      customer.tags = tags.map(item => ({
        id: item.id,
        tagName: item.tagName
      }))
      return
    }

    if(fieldName === 'manager'){
      customer.customerManager = value.userId || '';
      customer.customerManagerName = value.displayName || ''
      return;
    }
    
    if (field.formType === 'address' && !field.isSystem) {
      let all = formatAddress(value,'');

      all && (value.all = all);
    }
    
    if ((field.formType === 'location') && !value.isHaveLocation) {
      value = {};
    }
    
    isSystem == 0
      ? customer.attribute[fieldName] = value
      : customer[fieldName] = value;
  });

  return customer;
}

/** 将客户对象转成form表单，用于初始化表单 */
export function packToForm(field, data, defaultAddress = {}){
  let cusAdr = data.customerAddress || defaultAddress || {};

  let attribute = {
    ...data.attribute
  };
  let form = {
    id: data.id,
    name: data.name,
    lmName: data.lmName,
    lmPhone: data.lmPhone,
    lmEmail: data.lmEmail,
    serialNumber: data.serialNumber,
    customerAddress: {
      country: cusAdr.adCountry || '',
      province: cusAdr.adProvince,
      city: cusAdr.adCity,
      dist: cusAdr.adDist,
      address: cusAdr.adAddress,
      longitude: cusAdr.adLongitude || '',
      latitude: cusAdr.adLatitude || '',
      addressType: cusAdr.addressType || 0,
      street: cusAdr.adStreet || '',
    },
    tags: toArray(data.tags || []),
    manager: data.customerManager ? {displayName: data.customerManagerName, userId: data.customerManager, staffId: data.customerManagerStaffId} : null,
    synergies: data.synergies,
    parentCustomer: data?.parentCustomer || null,
  };

  return Object.assign(attribute, form)
}

// 客户标签hover提示数据处理
export function customerTagValue(value) {
  if (!value || !Array.isArray(value) || !value.length) return '';

  return value
  .map(tag => tag.tagName).filter(Boolean)
  .join('，');
}

// 客户标签展示
export function customerLabel(value) {
  if (!Array.isArray(value) || value.length <= 0) return [];

  if (value.length <= 3) return value;

  let newTag = value.slice(0, 2)
  let lastTag = {
    tagId: null,
    tagName: `+${value.length - 2}`,
  }
  newTag.push(lastTag)

  return newTag
}